//
//  NSString+Format.m
//  HGMMT
//
//  Created by huan liu on 2018/12/13.
//  Copyright © 2018年 eport_user1. All rights reserved.
//

#import "NSString+Format.h"

@implementation NSString (Format)

-(NSString *)formatIpPorts{
    
    NSArray *allArray = [self componentsSeparatedByString:@"-"];
    NSString *text = allArray[0];
    NSMutableArray *mutaArry = [NSMutableArray array];
    if (text.length == 7) {
        NSRange range1 = {0,1};
        NSString *string1 = [text substringWithRange:range1];
        [mutaArry addObject:string1];
        NSRange range2 = {1,2};
        NSString *string2 = [text substringWithRange:range2];
        [mutaArry addObject:string2];
        NSRange range3 = {3,2};
        NSString *string3 = [text substringWithRange:range3];
        [mutaArry addObject:string3];
        NSRange range4 = {5,2};
        NSString *string4 = [text substringWithRange:range4];
        [mutaArry addObject:string4];
        
        
    }else if (text.length == 8){
        
        NSRange range1 = {0,2};
        NSString *string1 = [text substringWithRange:range1];
        [mutaArry addObject:string1];
        NSRange range2 = {2,2};
        NSString *string2 = [text substringWithRange:range2];
        [mutaArry addObject:string2];
        NSRange range3 = {4,2};
        NSString *string3 = [text substringWithRange:range3];
        [mutaArry addObject:string3];
        NSRange range4 = {6,2};
        NSString *string4 = [text substringWithRange:range4];
        [mutaArry addObject:string4];
        
    }
    
    
    NSMutableArray *ips = [NSMutableArray array];
    
    for (NSString *str in mutaArry) {
        
        NSString *twoString = [self getBinaryByHex:str];
        NSString *tenString = [NSString stringWithFormat:@"%ld",[self getDecimalByBinary:twoString]];
        [ips addObject:tenString];
        
    }
    
    NSString *ipString = [ips componentsJoinedByString:@"."];
    NSString *twoPort = [self getBinaryByHex:allArray[1]];
    NSString *tenPort = [NSString stringWithFormat:@"%ld",[self getDecimalByBinary:twoPort]];
    
    NSString *result = [NSString stringWithFormat:@"%@:%@",ipString,tenPort];

    return result;
    
}


//十六进制转二进制
- (NSString *)getBinaryByHex:(NSString *)hex {
    
    NSMutableDictionary *hexDic = [[NSMutableDictionary alloc] initWithCapacity:16];
    [hexDic setObject:@"0000" forKey:@"0"];
    [hexDic setObject:@"0001" forKey:@"1"];
    [hexDic setObject:@"0010" forKey:@"2"];
    [hexDic setObject:@"0011" forKey:@"3"];
    [hexDic setObject:@"0100" forKey:@"4"];
    [hexDic setObject:@"0101" forKey:@"5"];
    [hexDic setObject:@"0110" forKey:@"6"];
    [hexDic setObject:@"0111" forKey:@"7"];
    [hexDic setObject:@"1000" forKey:@"8"];
    [hexDic setObject:@"1001" forKey:@"9"];
    [hexDic setObject:@"1010" forKey:@"A"];
    [hexDic setObject:@"1011" forKey:@"B"];
    [hexDic setObject:@"1100" forKey:@"C"];
    [hexDic setObject:@"1101" forKey:@"D"];
    [hexDic setObject:@"1110" forKey:@"E"];
    [hexDic setObject:@"1111" forKey:@"F"];
    
    NSString *binary = @"";
    for (int i=0; i<[hex length]; i++) {
        
        NSString *key = [hex substringWithRange:NSMakeRange(i, 1)];
        NSString *value = [hexDic objectForKey:key.uppercaseString];
        if (value) {
            
            binary = [binary stringByAppendingString:value];
        }
    }
    return binary;
}


/**
 二进制转换为十进制
 
 @param binary 二进制数
 @return 十进制数
 */
- (NSInteger)getDecimalByBinary:(NSString *)binary {
    
    NSInteger decimal = 0;
    for (int i=0; i<binary.length; i++) {
        
        NSString *number = [binary substringWithRange:NSMakeRange(binary.length - i - 1, 1)];
        if ([number isEqualToString:@"1"]) {
            
            decimal += pow(2, i);
        }
    }
    return decimal;
}


@end
